package com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentsupplier;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.excel.EasyExcel;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.RoomEquipmentCreateReq;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentsupplier.vo.*;
import com.xiaoshuidi.cloud.module.rooms.convert.equipmentsupplier.EquipmentSupplierConvert;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.mq.listener.RoomEquipmentListener;
import com.xiaoshuidi.cloud.module.rooms.mq.listener.RoomEquipmentSupplierListener;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentSupplier;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomEquipmentSupplierService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

/**
 *  资产管理-供应商管理
 *
 * @author Link
 * @since 2023-05-18
 */
@Tag(name = "管理后台 - 供应商管理")
@RestController
@RequestMapping(ApiConstants.NAME + "/roomequipmentsupplier")
@RequiredArgsConstructor
public class RoomEquipmentSupplierController {

    @Resource
    private IRoomEquipmentSupplierService iRoomEquipmentSupplierService;

    /**
     * 供应商管理分页
     */
    @Operation(summary = "供应商管理分页")
    @GetMapping("/pageQuery")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:supplier:query')")
    public CommonResult<PageResult<EquipmentSupplierRespVO>> getEquipmentSupplierPage(EquipmentSupplierPageReqVO pageVO) {
        PageResult<EquipmentSupplierRespVO> ret = iRoomEquipmentSupplierService.getEquipmentSupplierPage(pageVO);
        return success(ret);
    }
    /**
     * 创建供应商管理
     */
    @PostMapping("/create")
    @Operation(summary = "创建供应商管理")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:supplier:create')")
    public CommonResult<Long> createEquipmentSupplier(@RequestBody EquipmentSupplierCreateReqVO createReqVO) {
        return success(iRoomEquipmentSupplierService.createEquipmentSupplier(createReqVO));
    }
    /**
     * 更新供应商管理
     */
    @PutMapping("/update")
    @Operation(summary = "更新供应商管理")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:supplier:update')")
    public CommonResult<Boolean> updateEquipmentSupplier(@RequestBody EquipmentSupplierUpdateReqVO updateReqVO) {
        iRoomEquipmentSupplierService.updateEquipmentSupplier(updateReqVO);
        return success(true);
    }
    /**
     * 删除供应商管理
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:supplier:delete')")
    public CommonResult<Boolean> deleteEquipmentSupplier(@RequestParam("id") Long id) {
        iRoomEquipmentSupplierService.deleteEquipmentSupplier(id);
        return success(true);
    }
    /**
     * 获得供应商管理详情
     */
    @GetMapping("/get")
    @Operation(summary = "获得供应商管理详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:supplier:query')")
    public CommonResult<EquipmentSupplierRespVO> getEquipmentSupplier(@RequestParam("id") Long id) {
        EquipmentSupplier equipmentSupplier = iRoomEquipmentSupplierService.getEquipmentSupplier(id);
        EquipmentSupplierRespVO vo = EquipmentSupplierConvert.INSTANCE.convert(equipmentSupplier);
        vo.setCreateTimeStr(LocalDateTimeUtil.format(vo.getCreateTime(),"yyyy-MM-dd"));
        return success(vo);
    }

    /**
     * 批量上传供应商管理
     */
    @Operation(summary = "批量供应商管理")
    @PutMapping("/importRoomEquipmentSupplier")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "false")
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:equipment:supplier:import')")
    public CommonResult<Boolean> importRoomEquipmentSupplier(@RequestParam("file") MultipartFile file,
                                                     @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {

        EasyExcel.read(file.getInputStream(), EquipmentSupplierImportVO.class, new RoomEquipmentSupplierListener(iRoomEquipmentSupplierService)).sheet().headRowNumber(2).doRead();
        return success(true);
    }

}
